package com.qf.controller;

import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import java.text.SimpleDateFormat;
import java.util.Date;

@RestController
public class MQController {

    @Autowired
    private RabbitTemplate rabbitTemplate;

    @GetMapping("/send")
    @ResponseBody
    public String send(String msg) {
        // msg消息分为两位，error(模拟消费者消费不了的)和非error(可以消费)
        rabbitTemplate.convertAndSend("businessExchange", "emp.add", msg);
        return msg;
    }

    @GetMapping("/ttl")
    @ResponseBody
    public String ttl(String msg) {
        String routingKey = null;
        String format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date());
        System.out.println("消息发送的时间是:"+format);
        if ("10".equals(msg)) {
            routingKey = "ttl.10.send";
        } else if ("5".equals(msg)) {
            routingKey = "ttl.5.send";
        }
        rabbitTemplate.convertAndSend("businessExchange", routingKey, msg);
        return msg;
    }
}
